<?php
/**
 * @author sg
 * @date 2017/11/29
 */

namespace app\admin\controller;
use app\admin\model\AssetsUser;
use app\admin\model\JsCustomer;
use app\admin\model\StockApply;
use app\admin\model\StockMoneyAllot;
use app\admin\model\CompanyMoneyAllot;
use app\admin\model\Company;
use app\admin\model\StockMoneyApply;
use app\admin\model\StockMoneyApplyDetailed;
use app\admin\model\CompanyIncome;
use app\admin\model\CompanyManageAccout;
use app\admin\model\MoneyHistory;
use app\admin\model\AssetsHelp;
use app\admin\model\FilialeTodoModel;
class Profit extends Common
{
    public $whereSource = array('in', '1,2');
    public function index()
    {
        if ($_POST || !$_GET['page']) {
            session('searchParam', null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['uname'] = @$_POST['uname'] ? $_POST['uname'] : @$sessionParam['uname'];
        $sessionArr['tname'] = @$_POST['tname'] ? $_POST['tname'] : @$sessionParam['tname'];
        $sessionArr['start'] = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end'] = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam', $sessionArr);
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $where = "";
        if ($sessionArr['uname']) {
            $where .= " and c.username= " ."'". $sessionArr['uname']."'" ;
        }
        //获取truename
        if ($sessionArr['tname']) {
            $where .= " and c.truename= " ."'".  $sessionArr['tname'] ."'";
        }
        if ($sessionArr['start'] && $sessionArr['end']) {
            $where .= " and sma.addtime >=" . $sessionArr['start'] . " 00:00:00" . "and sma.addtime <=" . $sessionArr['end'] . " 23:59:59";
        }
        $startNum = ($page - 1) * $this->pernum;
        //获取股东分红
        $smaModel = new StockMoneyAllot();
        $stockSql = "SELECT c.username,c.truename,sma.uid,sma.money,sma.addtime,sma.stocknum,sma.allot_per,sa.stocktypeid FROM stockapply sa";
        $stockSql .= " LEFT JOIN customer c ON sa.customerId = c.id ";
        $stockSql .= " LEFT JOIN stock_money_allot sma ON sa.id = sma.said ";
        $stockSql .= " where sa.companyid = " . session('company_id') . " and sma.cpid = " . session('company_id') . " AND sa.approvestateid = 4 " . $where;
        $stockSql .= " ORDER BY sma.Id DESC LIMIT {$startNum} ,{$this->pernum}";

        $thList = $smaModel->queryData($stockSql);
        $stockSqlcount = "SELECT count(1) as num FROM stockapply sa";
        $stockSqlcount .= " LEFT JOIN customer c ON sa.customerId = c.id ";
        $stockSqlcount .= " LEFT JOIN stock_money_allot sma ON sa.id = sma.said ";
        $stockSqlcount .= " where sa.companyid = " . session('company_id') . " and sma.cpid = " . session('company_id') . " AND sa.approvestateid = 4 " . $where;

        $totalNum = $smaModel->queryData($stockSqlcount);
        $pageShow = $smaModel->paginate($this->pernum, $totalNum[0]['num'])->render();
        $this->assign('pageShow', $pageShow);
        $this->assign('page', $page);
        $this->assign('resList', $thList);
        $this->assign('postArr', $sessionArr);
        $tmpdata = array('1' => '省级股东', '2' => '市级股东', '3' => '县区级股东');
        $this->assign('stockRes', $tmpdata);

        $companyCreateOn = session('company_create_time');
        $dateNow = date('Y-m-d H:i:s');

        $companyIncomeModel = new CompanyIncome();
        $wheredata = array();
        $wheredata['cpid'] = array('eq', session('company_id'));
        $wheredata['addtime'] = array(array('egt', $companyCreateOn), array('elt', $dateNow));
        $companySum =  number_format($companyIncomeModel->sumData($wheredata,'money'),2);

        $this->assign('companySum', $companySum);
        $wheredata['source'] = 1;
        $cbSum = number_format($companyIncomeModel->sumData($wheredata,'money'),2);
        $this->assign('cbSum', $cbSum);
        $wheredata['source'] = 2;
        $scSum = number_format($companyIncomeModel->sumData($wheredata,'money'),2);
        $this->assign('scSum', $scSum);
        $wheredata['source'] = 3;
        $tjSum = number_format($companyIncomeModel->sumData($wheredata,'money'),2);
        $this->assign('tjSum', $tjSum);
        $wheredata['source'] = 4;
        $zbSum = number_format($companyIncomeModel->sumData($wheredata,'money'),2);
        $this->assign('zbSum', $zbSum);
        $wheredata['source'] = 5;
        $yxSum = number_format($companyIncomeModel->sumData($wheredata,'money'),2);
        $this->assign('yxSum', $yxSum);

        $companyModel = new Company();
        $companyData  = $companyModel->findData(['id'=>session('company_id')]);
        if($companyData['last_allot_time'] == null)
        {
            $stime = $companyData['CreateOn'];
        }
        else
        {

            $stime = $companyData['last_allot_time'];
        }

        $dateNow = date('Y-m-d H:i:s');

        $companyIncomeModel = new CompanyIncome();
        $wheredata = array();
        $wheredata['cpid'] = array('eq', session('company_id'));
        $wheredata['addtime'] = array(array('egt', $stime), array('elt', $dateNow));
        $wheredata['source'] = $this->whereSource;

        $allotSum = $companyIncomeModel->sumData($wheredata,'money');

        $allotMoney = number_format($allotSum * 0.49 * ($companyData['allot_percent'] / 100),2);

        $this->assign('allotMoney', $allotMoney);

        $leftMoney = number_format($companyData['LeftMoney'],2);
        $this->assign('leftMoney', $leftMoney);

        return $this->fetch();
    }

    public function StockMoneyAllotList()
    {
        if ($_POST || !$_GET['page']) {
            session('searchParam', null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['uname'] = @$_POST['uname'] ? $_POST['uname'] : @$sessionParam['uname'];
        $sessionArr['tname'] = @$_POST['tname'] ? $_POST['tname'] : @$sessionParam['tname'];
        $sessionArr['start'] = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end'] = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam', $sessionArr);
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $where = [];
        $where['cpid'] = session('company_id');
        if ($sessionArr['start'] && $sessionArr['end']) {
            $where['addtime'] = ['between', [$sessionArr['start'], $sessionArr['end'] . ' 23:59:59']];
        }
        $companyMoneyAllotModel = new CompanyMoneyAllot();
        $resList = $companyMoneyAllotModel->searchData($where, $page, $this->pernum, 'Id desc');
        $company_type = session('company_type');

        foreach ($resList as $k => $v) {
            $smaModel = new StockMoneyAllot();
            $stockSql = " SELECT c.username,c.truename,sma.money,sma.stocknum,'{$company_type}' AS stocktype FROM stock_money_allot sma ";
            $stockSql .= " LEFT JOIN customer c ON sma.uid = c.id";
            $stockSql .= " WHERE sma.cpid=" . $where['cpid'] . " AND maid=" . $v[id];
            if ($sessionArr['uname']) {
                $stockSql .= " and username = " . "'" . $sessionArr['uname'] . "'";
            }
            if ($sessionArr['tname']) {
                $stockSql .= " and truename = " . "'" . $sessionArr['tname'] . "'";
            }
            $subsetList = $smaModel->queryData($stockSql);
            $resList[$k]['kk'] = $subsetList;
        }
        $this->assign('group', $resList);

        $totalNum = $companyMoneyAllotModel->getTotalNum($where);
        $pageShow = $companyMoneyAllotModel->paginate($this->pernum, $totalNum)->render();
        $this->assign('pageShow', $pageShow);
        $this->assign('page', $page);
        $this->assign('resList', $resList);
        $this->assign('postArr', $sessionArr);
        $tmpdata = array('1' => '省级股东', '2' => '市级股东', '3' => '县区级股东');
        $this->assign('stockRes', $tmpdata);
        return $this->fetch();
    }

    //分红申请
    public function apply()
    {
        $companyId = session('company_id');
        $where['Id'] = $companyId;
        $companyModel = new Company();
        $companyData = $companyModel->findData($where, 'id,allot_model,allot_percent,last_allot_time');
        if ($companyData) {
            //获取公司上次分红时间，如果为空，则选择公司成立时间
            $allotmodel = $companyData['allot_model'];
            if ($companyData['last_allot_time'] != null) {
                $stime = $companyData['last_allot_time'];
            } else {
                $stime = session("company_create_time");
            }
            $time = strtotime($stime);
            switch ($allotmodel) {
                case 1: //获取可分红年时间
                    $etime = date('Y', $time) + 1 . '-' . date("m-d", $time) . ' ' . '23:59:59';
                    break;
                case 2://获取可分红季度时间
                    //如果上次分红时间为空，择选用公司注册时间做为开始时间，结束时间为本月上季度最后一天23:59:59
                    if ($companyData['last_allot_time'] == null) {
                        $season = ceil((date('n')) / 3) - 1;
                        $etime = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
                    } else {
                        //根据上次分红时间，获取下个季度分红的结束时间
                        $season = ceil((date("m", $time) + 1) / 3);
                        $etime = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
                    }
                    break;
                default://获取可分红月时间
                    if ($companyData['last_allot_time'] == null) {
                        //公司上次分红时间为空，则获取上月最后一天23:59:59
                        $etime = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")));
                    } else {
                        //根据上次分红日期获取本次分红结束日期
                        $firstdaystr = date("Y-m-01", $time);
                        $etime = date('Y-m-d', strtotime("$firstdaystr + 2 month -1 day")) . ' 23:59:59';

                        $date = date('Y-m-d H:i:s'); //当前时间
                        $firstday = date('Y-m-01', strtotime($date));
                        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
                        //如果隔月分红择，选用上月月底时间23:59:59
                        if (strtotime($lastday) > strtotime($etime)) {
                            $etime = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")));
                        }
                    }
            }
            //判断是否已经存在提交申请
            $stockMoneyApplyModel = new  StockMoneyApply();
            $smadata = $stockMoneyApplyModel->findData(['cpid' => $companyId, 'state' => 0]);
            if ($smadata) {
                return "已经提交，等待审核！";
            }
            //判断是否在分红时间内
            if (strtotime(date('Y-m-d H:i:s')) < strtotime($etime)) {
                return "请在<span style=color:red>" . $etime . "</span>之后再次提交分红申请！";
            } else {
                $companyIncomeModel = new CompanyIncome();
                $wheredata = array();
                $wheredata['cpid'] = array('eq', $companyId);
                $wheredata['addtime'] = array(array('egt', $stime), array('elt', $etime));
                $wheredata['source'] = $this->whereSource;
                //获取可分红的销售总额

                $company_money = $companyIncomeModel->sumData($wheredata,'money');
                $company_dividend = $company_money * 0.49 * ($companyData['allot_percent'] / 100); //可分红金额
                $stockMoneyApplyModel->startTrans();
                try {
                    $data = [];
                    $data['cpid'] = $companyId;
                    $data['initiator_id'] = session('user_id');
                    $data['allot_money'] = $company_dividend;
                    $data['money'] = $company_money;
                    $data['allot_model'] = $allotmodel;
                    $data['allot_per'] = $companyData['allot_percent'];
                    $data['stime'] = $stime;
                    $data['etime'] = $etime;
                    $data['apply_time'] = date('Y-m-d H:i:s');
                    $getLastInsID = $stockMoneyApplyModel->addData($data); //添加分红申请
                    $stockApplyModel = new StockApply();
                    $sql = "SELECT  a.id,a.customerid,c.username,c.truename,a.stocktypeid,a.stocknum,a.checkedon FROM stockapply a";
                    $sql .= " LEFT JOIN customer c ON a.customerid = c.id ";
                    $sql .= " WHERE a.companyId =" . $companyId . " AND a.ApproveStateId = 4";
                    //获取本公司股东
                    $stockApplyList = $stockApplyModel->queryData($sql);
                    foreach ($stockApplyList as $val) {
                        //成为股东时间大于分红开始时间则用成股东时间
                        if (strtotime($val['checkedon']) > $time) {
                            $stime = $val['checkedon'];
                        } else {
                            $stime = date('Y-m-d H:i:s', $stime);
                        }
                        //股东时间大于分红结束时间分红金额为0
                        if (strtotime($stime) > strtotime($etime)) {
                            $storckMoney = 0;
                        } else {
                            //获取每个股东分红金额
                            $wherestockk = array();
                            $wherestockk['cpid'] = array('eq', $companyId);
                            $wherestockk['addtime'] = array(array('egt', $stime), array('elt', $etime));
                            $wherestockk['source'] = $this->whereSource;

                            $company_money_stock = $companyIncomeModel->sumData($wherestockk,'money');
                            $company_money_stock = $company_money_stock * 0.49 * ($companyData['allot_percent'] / 100);
                            $storckMoney = round($company_money_stock * ($val['stocknum'] / 49), 2);
                        }
                        $datadetailed[] = array(
                            'apply_id' => $getLastInsID,
                            'customerId' => $val['customerid'],
                            'username' => $val['username'],
                            'truename' => $val['truename'],
                            'stockTypeId' => $val['stocktypeid'],
                            'stocknum' => $val['stocknum'],
                            'allot_money' => $storckMoney,
                            'jointime' =>$val['checkedon'],
                            'said'=>$val['id'],
                            'allot_per'=>$companyData['allot_percent']
                        );
                    }
                    //添加本次股东分红明细
                    $stockMoneyApplyDetailedModel = new StockMoneyApplyDetailed();
                    $stockMoneyApplyDetailedModel->insertAll($datadetailed);

                    $companyManageAccoutModel = new  CompanyManageAccout();
                    $companyManageAccoutData = $companyManageAccoutModel->findData(['cpid'=>$companyId,'type'=>1,'status'=>1]);

                    if($companyManageAccoutData)
                    {
                        $filialeTodoModel = new  FilialeTodoModel();
                        $filialeTodoModel->addData(['cpid'=>$companyId,'manageid'=>$companyManageAccoutData['id'],'message'=>'您有股东分红需要审核','gotoUrl'=>'/admin/profit/checklist','goId'=>$getLastInsID]);
                    }

                } catch (Exception $e) {
                    $stockMoneyApplyModel->rollback();
                } finally {
                    $stockMoneyApplyModel->commit();
                    return "分红提交成功!";
                }
            }
        } else {
            return "公司不存在！";
        }
    }

    //分红审核列表
    public function checkList()
    {
        if ($_POST || !$_GET['page']) {
            session('searchParam', null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['status'] = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['statustype'] = @$_POST['statustype'] ? $_POST['statustype'] : @$sessionParam['statustype'];
        $sessionArr['start'] = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end'] = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam', $sessionArr);
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $where = [];
        if ($sessionArr['status'] == "待审核") {
            $where['state'] = 0;
        } elseif ($sessionArr['status'] == "审核通过") {
            $where['state'] = 1;
        } elseif ($sessionArr['status'] == "审核不通过") {
            $where['state'] = 2;
        }
        if ($sessionArr['statustype'] == "按季度分红") {
            $where['allot_model'] = 2;
        } elseif ($sessionArr['statustype'] == "按月分红") {
            $where['allot_model'] = 3;
        } elseif ($sessionArr['statustype'] == "按年分红") {
            $where['allot_model'] = 1;
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['apply_time'] = ['between',[$sessionArr['start'],$sessionArr['end'].' 23:59:59']];
        }
        $where['cpid'] = session('company_id');
        $stockMoneyApplyModel = new StockMoneyApply();
        $resList = $stockMoneyApplyModel->searchData($where,$page,$this->pernum,'Id desc');
        $totalNum = $stockMoneyApplyModel->getTotalNum($where);
        $pageShow = $stockMoneyApplyModel->paginate($this->pernum,$totalNum)->render();
        $companyManageAccoutModel = new CompanyManageAccout();
        $fArr = [];
        $sArr = [];
        foreach ($resList as $val){
            $fArr[] = $val['initiator_id'];
            $sArr[] = $val['auditor'];
        }
        $uRes = [];
        if($fArr){
            $whereManage = [];
            $whereManage['cpid'] = session('company_id');
            $whereManage['Id'] = ['in',$fArr];

            $companyManageAccoutData = $companyManageAccoutModel->selectData($whereManage,'id,realname',['id'=>'asc']);
            foreach ($companyManageAccoutData as $val){
                $uRes[$val['id']]['realname'] = $val['realname'];
            }
        }
        $aRes = [];
        if($sArr){
            $whereManage = [];
            $whereManage['cpid'] = session('company_id');
            $whereManage['Id'] = ['in',$sArr];

            $companyManageAccoutData = $companyManageAccoutModel->selectData($whereManage,'id,realname',['id'=>'asc']);
            foreach ($companyManageAccoutData as $val){
                $aRes[$val['id']]['realname'] = $val['realname'];
            }
        }
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('postArr',$sessionArr);
        $tmpdata =  array('1'=>'年分红','2'=>'季度分红','3'=>'月分红');
        $this->assign('Res',$tmpdata);
        $checkdata =  array('0'=>'待审核','1'=>'审核通过','2'=>'审核不通过');
        $this->assign('checkRes',$checkdata);
        $this->assign('ManageRes',$uRes);
        $this->assign('AuditorRes',$aRes);

        return $this->fetch();
    }

    //分红审核
    public function checkdetailed()
    {

        if($_POST){
            $id = $_POST['id'];
            $stockMoneyApplyModel = new StockMoneyApply();
            $stockMoneyApplyModel->editData(['Id'=>$id],['audit_opinion'=>$_POST['secmemo'],'auditor'=>session('user_id'),'audit_time'=>date('Y-m-d H:i:s'),'state'=>$_POST['state']]);
            if($_POST['state'] == 1) {
                $stockMoneyApplyDetailedModel = new StockMoneyApplyDetailed();
                $stockMoneyApplyData = $stockMoneyApplyModel->findData(['id' => $id]);
                $stockMoneyApplyDetailedData = $stockMoneyApplyDetailedModel->selectData(['apply_id' => $id]);

                if ($stockMoneyApplyData && $stockMoneyApplyDetailedData) {
                    try {
                        //本次分红金额
                        $stockMoneySum = $stockMoneyApplyDetailedModel->sumData(['apply_id' => $id], 'allot_money');
                        $companyMoneyAllotModel = new CompanyMoneyAllot();
                        //获取上次第几次分配
                        $companyMoneyAllotAllotNum = $companyMoneyAllotModel->findData(['cpid' => $stockMoneyApplyData['cpid']], ['allot_num'], ['id' => 'desc']);
                        //本次分红剩余金额
                        $LeftMoneyAmount = $stockMoneyApplyData['allot_money'] - $stockMoneySum;
                        //本次云路通金额
                        $yunlutongMoney = $stockMoneyApplyData['money'] * 0.51;
                        $companyMoneyAllotArr = [];
                        $companyMoneyAllotArr['cpid'] = $stockMoneyApplyData['cpid'];
                        $companyMoneyAllotArr['allot_money'] = $stockMoneyApplyData['allot_money'];
                        $companyMoneyAllotArr['allot_money_done'] = $stockMoneySum;
                        $companyMoneyAllotArr['stime'] = $stockMoneyApplyData['stime'];
                        $companyMoneyAllotArr['etime'] = $stockMoneyApplyData['etime'];
                        $companyMoneyAllotArr['addtime'] = date('Y-m-d H:i:s');
                        $companyMoneyAllotArr['allot_num'] = $companyMoneyAllotAllotNum['allot_num'] = NULL ? 1 : $companyMoneyAllotAllotNum['allot_num'] + 1;
                        $companyMoneyAllotArr['allot_per'] = $stockMoneyApplyData['allot_per'];

                        $companyMoneyAllotModel->startTrans();
                        $companyMoneyAllotLastInsID = $companyMoneyAllotModel->addData($companyMoneyAllotArr);
                        $stockMoneyAllotModel = new  StockMoneyAllot();
                        $moneyHistoryModel = new MoneyHistory();
                        foreach ($stockMoneyApplyDetailedData as $val) {
                            $stockMoneyAllotArr = [];
                            $stockMoneyAllotArr['uid'] = $val['customerId'];
                            $stockMoneyAllotArr['money'] = $val['allot_money'];
                            $stockMoneyAllotArr['maid'] = $companyMoneyAllotLastInsID;
                            $stockMoneyAllotArr['cpid'] = $stockMoneyApplyData['cpid'];
                            $stockMoneyAllotArr['addtime'] = date('Y-m-d H:i:s');
                            $stockMoneyAllotArr['jointime'] = $val['jointime'];
                            $stockMoneyAllotArr['stocknum'] = $val['stocknum'];
                            $stockMoneyAllotArr['allot_per'] = $val['allot_per'];
                            $stockMoneyAllotArr['said'] = $val['said'];
                            $stockMoneyAllotModel->addData($stockMoneyAllotArr);

                            if ($val['allot_money'] > 0) {
                                $moneyHistoryMoneyBalance = $moneyHistoryModel->findData(['CustomerId' => $val['customerId'], 'appid' => 8], ['moneybalance'], ['id' => 'desc']);
                                $moneyHistoryArr = [];
                                $moneyHistoryArr['Money'] = $val['allot_money'];
                                $moneyHistoryArr['MoneyBalance'] = $moneyHistoryMoneyBalance['moneybalance'] = NULL ? $val['allot_money'] : $moneyHistoryMoneyBalance['moneybalance'] + $val['allot_money'];
                                $moneyHistoryArr['IsFreezed'] = 0;
                                $moneyHistoryArr['Message'] = '股东分红';
                                $moneyHistoryArr['CreateOn'] = date('Y-m-d H:i:s');
                                $moneyHistoryArr['AppID'] = 8;
                                $moneyHistoryArr['CustomerId'] = $val['customerId'];
                                $moneyHistoryArr['Type'] = 0;
                                $moneyHistoryModel->addData($moneyHistoryArr);

                                $assetsHelpModel = new AssetsHelp();
                                $ahiMemo = '尊敬的云路通东家您好，恭喜您获得云路通东家的元宝'.$val['allot_money'].'已经到账';

                                $assetsUserModel = new  AssetsUser();
                                $assetsUserModel->AssetsUserAdd($val['customerId'],8,$val['allot_money']);
                                $item = [
                                    ['ahkey'=>'收益金额','ahval'=> $val['allot_money'].'元宝'],
                                    ['ahkey'=>'日期区间','ahval'=> $stockMoneyApplyData['stime'].'至'.$stockMoneyApplyData['etime']],
                                    ['ahkey'=>'收益来源','ahval'=>'公司股东分红'],
                                    ['ahkey'=>'公司名称','ahval'=> session('company_name').'分公司']
                                ];
                                $assetsHelpModel->assetsHelpItemAdd($val['customerId'],'股东收益',$ahiMemo,$item);
                            }
                        }
                        $companyModel = new Company();
                        $companySql = "UPDATE company SET OperateMoney = OperateMoney +" . $stockMoneySum . ",yltLeftMoney = yltLeftMoney +" . $yunlutongMoney;
                        $companySql .= " ,LeftMoney = LeftMoney + " . $LeftMoneyAmount . ",last_allot_time =" . "'" . $stockMoneyApplyData['etime'] . "'";
                        $companySql .= " WHERE id= " . $stockMoneyApplyData['cpid'];
                        $companyModel->execute($companySql);

                        $filialeTodoModel = new  FilialeTodoModel();
                        $filialeTodoModel->editData(['goId'=>$id],['state'=>1]);

                    } catch (Exception $e) {
                        $companyMoneyAllotModel->rollback();
                    } finally {
                        $companyMoneyAllotModel->commit();
                    }
                }
            }
            $this->redirect('/admin/profit/checklist?page='.$_POST['page']);
        }
        $id = $_GET['id'];
        $stockMoneyApplyModel = new StockMoneyApply();
        $stockMoneyApplyData = $stockMoneyApplyModel->findData(['Id'=>$id]); //获取分红申请表
        $companyManageAccoutModel = new CompanyManageAccout();
        $initiator_id =  $stockMoneyApplyData['initiator_id'];
        if($initiator_id != '')
        {
            $manageRes = $companyManageAccoutModel->findData(['Id'=>$initiator_id],['realname']);
        }
        $hRes = $companyManageAccoutModel->findData(['Id'=>session('user_id')],['realname']);
        $stockMoneyApplyDetailedModel = new  StockMoneyApplyDetailed();
        $stockMoneyApplyDetailedData = $stockMoneyApplyDetailedModel->selectData(['apply_id'=>$id]); //获取分红申请明细

        $this->assign('ApplyRes',$stockMoneyApplyData);
        $this->assign('ManageRes',$manageRes);
        $this->assign('HRes',$hRes);
        $tmpdata =  array('1'=>'年分红','2'=>'季度分红','3'=>'月分红');
        $this->assign('Res',$tmpdata);
        $this->assign('resList',$stockMoneyApplyDetailedData);
        $this->assign('page',$_GET['page']);
        return $this->fetch();
    }
    public function checkdetails()
    {
        if($_POST || !$_GET['page']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['id']  = @$_POST['id'] ? $_POST['id'] : @$_GET['id'];
        session('searchParam',$sessionArr);
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $where = [];
        $where['Id'] = $sessionArr['id'];
        //获取分红申请表
        $stockMoneyApplyModel = new StockMoneyApply();
        $stockMoneyApplyData = $stockMoneyApplyModel->findData($where);
        $initiator_id =  $stockMoneyApplyData['initiator_id'];
        $auditor = $stockMoneyApplyData['auditor'];

        $companyManageAccoutModel = new CompanyManageAccout();
        if($initiator_id != '')
        {
            //获取提交人姓名
            $submitterName = $companyManageAccoutModel->findData(['Id'=>$initiator_id],['realname']);
        }
        if ($auditor !='')
        {
            //获取审核姓名
            $auditorName = $companyManageAccoutModel->findData(['Id'=>$auditor],['realname']);
        }
        $where = [];
        $where['apply_id'] = $sessionArr['id'];
        $stockMoneyApplyDetailedModel = new  StockMoneyApplyDetailed();
        $resList = $stockMoneyApplyDetailedModel->searchData($where,$page,$this->pernum,'Id desc');
        $totalNum = $stockMoneyApplyDetailedModel->getTotalNum($where);
        $pageShow = $stockMoneyApplyDetailedModel->paginate($this->pernum,$totalNum,['query'=>['id'=>$sessionArr['id']]])->render();
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $tmpdata =  array('1'=>'年分红','2'=>'季度分红','3'=>'月分红');
        $this->assign('Res',$tmpdata);
        $verifydata = array('0'=>'待审核','1'=>'审核通过','2'=>'审核未通过');
        $this->assign('verifyData',$verifydata);
        $this->assign('ApplyRes',$stockMoneyApplyData);
        $this->assign('submitterName',$submitterName);
        $this->assign('auditorName',$auditorName);
        return $this->fetch();
    }
}